//
//  JMDetailView.m
//  OpenSesame
//
//  Created by zhangjiaming on 16/1/5.
//  Copyright © 2016年 OpenSesame. All rights reserved.
//

#import "JMDetailView.h"
@interface JMDetailView () <UITextViewDelegate>

@property (weak, nonatomic) UIView *view5;
@property (weak, nonatomic) UIView *view6;
@property (weak, nonatomic) UIView *view7;
@property (weak, nonatomic) UIView *view8;
@property (weak, nonatomic) UIView *view9;

@property (weak, nonatomic) UILabel *label5;
@property (weak, nonatomic) UILabel *label6;

@property (weak, nonatomic) UIImageView *imageView5;
@property (weak, nonatomic) UIImageView *imageView6;

@end

@implementation JMDetailView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 第一行view
        CGFloat viewW = JMPhoneWidth;
        CGFloat viewH = 1;
        CGFloat viewx = 0;
        CGFloat interval = 10; // 间隔
        UIView *view1 = [[UIView alloc] init];
        [self addSubview:view1];
        view1.frame = CGRectMake(viewx, 0, viewW, viewH);
        view1.backgroundColor = JMColor(245, 245, 245);
        
        CGFloat imageViewH = 17;
        CGFloat imageViewW = 17;
        CGFloat imageViewX = 10;
        UIImageView *imageView1 = [[UIImageView alloc] init];
        [self addSubview:imageView1];
        imageView1.frame = CGRectMake(imageViewX, interval, imageViewW, imageViewH);
        imageView1.image = [UIImage imageNamed:@"频率"];
        
        CGFloat labelX = 37;
        CGFloat labelH = 37;
        CGFloat labelW = 100;
        UILabel *label1 = [[UILabel alloc] init];
        [self addSubview:label1];
        label1.textColor = JMColor(53, 53, 53);
        label1.font = [UIFont systemFontOfSize:13];
        label1.text = @"频率:";
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        attributes[NSFontAttributeName] = [UIFont systemFontOfSize:13];
        CGFloat label1W = [label1.text sizeWithAttributes:attributes].width;
        label1.frame = CGRectMake(labelX, view1.y, label1W, labelH);
        
        // 频率label
        UILabel *frequencyLabel = [[UILabel alloc] init];
        self.frequencyLabel = frequencyLabel;
        [self addSubview:frequencyLabel];
        CGFloat maxlabel1X = CGRectGetMaxX(label1.frame);
        frequencyLabel.frame = CGRectMake(label1.x + label1.width + 10, label1.y, JMPhoneWidth - maxlabel1X - 20, labelH);
        frequencyLabel.font = [UIFont systemFontOfSize:14];
        frequencyLabel.textAlignment = NSTextAlignmentRight;
        frequencyLabel.textColor = JMColor(51, 51, 51);
        
        // 第二行view
        UIView *view2 = [[UIView alloc] init];
        [self addSubview:view2];
        view2.frame = CGRectMake(viewx, frequencyLabel.y + frequencyLabel.height, viewW, viewH);
        view2.backgroundColor = JMColor(245, 245, 245);
        
        UIImageView *imageView2 = [[UIImageView alloc] init];
        [self addSubview:imageView2];
        imageView2.frame = CGRectMake(imageViewX, interval + view2.y + view2.height, imageViewW, imageViewH);
        imageView2.image = [UIImage imageNamed:@"适配子机"];
        
        UILabel *label2 = [[UILabel alloc] init];
        [self addSubview:label2];
        label2.textColor = JMColor(53, 53, 53);
        label2.font = [UIFont systemFontOfSize:13];
        label2.text = @"适配子机:";
        label2.frame = CGRectMake(labelX, view2.y, labelW, labelH);
        
        // 适配子机label
        UILabel *fitLabel = [[UILabel alloc] init];
        self.fitLabel = fitLabel;
        [self addSubview:fitLabel];
        fitLabel.frame = CGRectMake(label2.x + label2.width + 10, label2.y, JMPhoneWidth - label2.x - label2.width - 20, labelH);
        fitLabel.font = [UIFont systemFontOfSize:14];
        fitLabel.textAlignment = NSTextAlignmentRight;
        fitLabel.textColor = JMColor(51, 51, 51);
        
        // 第三行
//        UIView *view3 = [[UIView alloc] init];
//        [self addSubview:view3];
//        view3.frame = CGRectMake(viewx, fitLabel.y + fitLabel.height, viewW, viewH);
//        view3.backgroundColor = JMColor(245, 245, 245);
//        
//        UIImageView *imageView3 = [[UIImageView alloc] init];
//        [self addSubview:imageView3];
//        imageView3.frame = CGRectMake(imageViewX, interval + view3.y + view3.height, imageViewW, imageViewH);
//        imageView3.image = [UIImage imageNamed:@"可选功能"];
//        
//        UILabel *label3 = [[UILabel alloc] init];
//        [self addSubview:label3];
//        label3.textColor = JMColor(53, 53, 53);
//        label3.font = [UIFont systemFontOfSize:13];
//        label3.text = @"可选功能:";
//        label3.frame = CGRectMake(labelX, view3.y, labelW, labelH);
//        
//        // 可选功能label
//        UILabel *optionalLabel = [[UILabel alloc] init];
//        self.optionalLabel = optionalLabel;
//        [self addSubview:optionalLabel];
//        optionalLabel.frame = CGRectMake(label3.x + label3.width + 10, label3.y, JMPhoneWidth - label3.x - label3.width - 20, labelH);
//        optionalLabel.font = [UIFont systemFontOfSize:14];
//        optionalLabel.textAlignment = NSTextAlignmentRight;
//        optionalLabel.textColor = JMColor(51, 51, 51);
        
        // 第四行
        UIView *view4 = [[UIView alloc] init];
        [self addSubview:view4];
        view4.frame = CGRectMake(viewx, fitLabel.y + fitLabel.height, viewW, viewH);
        view4.backgroundColor = JMColor(245, 245, 245);

        UIImageView *imageView4 = [[UIImageView alloc] init];
        [self addSubview:imageView4];
        imageView4.frame = CGRectMake(imageViewX, interval + view4.y + view4.height, imageViewW, imageViewH);
        imageView4.image = [UIImage imageNamed:@"厂商说明"];
        
        UILabel *label4 = [[UILabel alloc] init];
        [self addSubview:label4];
        label4.textColor = JMColor(53, 53, 53);
        label4.font = [UIFont systemFontOfSize:13];
        label4.text = @"厂商说明:";
        label4.frame = CGRectMake(labelX, view4.y, labelW, labelH);
        
        // 第五行
        UIView *view5 = [[UIView alloc] init];
        [self addSubview:view5];
        view5.frame = CGRectMake(viewx, label4.y + label4.height, viewW, viewH);
        view5.backgroundColor = JMColor(245, 245, 245);
        self.view5 = view5;
        
        // 厂商说明label
        UILabel *illustrateLabel = [[UILabel alloc] init];
        [self addSubview:illustrateLabel];
        self.illustrateLabel = illustrateLabel;
        illustrateLabel.numberOfLines = 0;
        illustrateLabel.font = [UIFont systemFontOfSize:14];
        illustrateLabel.textColor = JMColor(51, 51, 51);
        illustrateLabel.frame = CGRectMake(interval, view5.y + view5.height, JMPhoneWidth-20, 100);
        
        // 第六行
        UIView *view6 = [[UIView alloc] init];
        self.view6 = view6;
        [self addSubview:view6];
        view6.frame = CGRectMake(viewx, self.illustrateLabel.y + self.illustrateLabel.height, viewW, viewH);
        view6.backgroundColor = JMColor(245, 245, 245);
        
        UIImageView *imageView5 = [[UIImageView alloc] init];
        [self addSubview:imageView5];
        imageView5.frame = CGRectMake(imageViewX, interval + view6.y + view6.height, imageViewW, imageViewH);
        imageView5.image = [UIImage imageNamed:@"组-14"];
        self.imageView5 = imageView5;
        
        UILabel *label5 = [[UILabel alloc] init];
        [self addSubview:label5];
        label5.textColor = JMColor(53, 53, 53);
        label5.font = [UIFont systemFontOfSize:13];
        label5.text = @"个人备注:";
        label5.frame = CGRectMake(labelX, view6.y, labelW, labelH);
        self.label5 = label5;
        
        // 第7行
        UIView *view7 = [[UIView alloc] init];
        [self addSubview:view7];
        view7.frame = CGRectMake(viewx, label5.y + label5.height, viewW, 1);
        view7.backgroundColor = JMColor(245, 245, 245);
        self.view7 = view7;
        
        // 个人备注textView
        UITextView *commentTextView = [[UITextView alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange) name:UITextViewTextDidChangeNotification object:commentTextView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewBeginChange) name:UITextViewTextDidBeginEditingNotification object:commentTextView];

        [self addSubview:commentTextView];
        self.commentTextView = commentTextView;
        commentTextView.delegate = self;
        commentTextView.frame = CGRectMake(0, view7.y + view7.height, JMPhoneWidth, 30);
        commentTextView.font = [UIFont systemFontOfSize:14];
        commentTextView.textColor = JMColor(51, 51, 51);
        commentTextView.tintColor = JMColor(153, 153, 153);
        commentTextView.returnKeyType = UIReturnKeyDone;
        
        // 第8行
        UIView *view8 = [[UIView alloc] init];
        [self addSubview:view8];
        view8.frame = CGRectMake(viewx, commentTextView.y + commentTextView.height, viewW, 1);
        view8.backgroundColor = JMColor(245, 245, 245);
        self.view8 = view8;
        
        UIImageView *imageView6 = [[UIImageView alloc] init];
        [self addSubview:imageView6];
        imageView6.frame = CGRectMake(imageViewX, interval + view8.y + view8.height, imageViewW, imageViewH);
        imageView6.image = [UIImage imageNamed:@"技术资料"];
        self.imageView6 = imageView6;
        
        UILabel *label6 = [[UILabel alloc] init];
        [self addSubview:label6];
        label6.textColor = JMColor(53, 53, 53);
        label6.font = [UIFont systemFontOfSize:13];
        label6.text = @"技术资料:";
        label6.frame = CGRectMake(labelX, view8.y, labelW, labelH);
        self.label6 = label6;
        
        // 第九行
        UIView *view9 = [[UIView alloc] init];
        [self addSubview:view9];
        view9.frame = CGRectMake(viewx, label6.y + label6.height, viewW, 1);
        view9.backgroundColor = JMColor(245, 245, 245);
        self.view9 = view9;
    }
    return self;
}

/**
 *  自适应改变高度
 */
- (void)textViewDidChange {
    CGSize commentTextViewSize = [self.commentTextView boundingRectWithSize:CGSizeMake(JMPhoneWidth, 0)];
    self.commentTextView.height = commentTextViewSize.height + 10;
    // 调整commentTextView下面的控件位置
    self.view8.y = self.commentTextView.y + self.commentTextView.height;
    self.imageView6.y = self.view8.y + self.view8.height + 10;
    self.label6.y = self.view8.y;
    self.view9.y = self.label6.y + self.label6.height;
    
    // 如果键盘遮挡，通知代理上移
    CGFloat maxY = CGRectGetMaxY(self.commentTextView.frame);
    if ([self.delegate respondsToSelector:@selector(detailView:moveUpTo:)]) {
        [self.delegate detailView:self moveUpTo:-(256.0 - (JMPhoneHeight - maxY - self.y))];
    }
}

/**
 *  滑出键盘上移
 */
- (void)textViewBeginChange {
    CGFloat maxY = CGRectGetMaxY(self.commentTextView.frame);
    if (JMPhoneHeight - maxY - self.y < 256.0) { // 弹出键盘上移
        // 如果键盘遮挡，通知代理上移
        if ([self.delegate respondsToSelector:@selector(detailView:moveUpTo:)]) {
            [self.delegate detailView:self moveUpTo:-(256.0 - (JMPhoneHeight - maxY - self.y))];
        }
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        // 通知代理发送请求添加个人备注
        if ([self.delegate respondsToSelector:@selector(detailView:moveDownTo:andRemark:)]) {
            [self.delegate detailView:self moveDownTo:0 andRemark:textView.text];
        }
        [self.commentTextView resignFirstResponder];
    }
    return YES;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
